package com.jcsz.gulimall.product.service.impl;

import com.alibaba.fastjson.util.AntiCollisionHashMap;
import com.jcsz.gulimall.product.entity.AttrGroupEntity;
import com.jcsz.gulimall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcsz.common.utils.PageUtils;
import com.jcsz.common.utils.Query;

import com.jcsz.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.jcsz.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.jcsz.gulimall.product.service.AttrAttrgroupRelationService;
import org.thymeleaf.expression.Ids;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBatch(List<AttrGroupRelationVo> attrGroupRelationVos) {

        List<AttrAttrgroupRelationEntity> collect = attrGroupRelationVos.stream().map((obj) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(obj, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());

        this.saveBatch(collect);
    }

    @Override
    public void deleteBatch(List<AttrGroupRelationVo> attrGroupRelationVos) {
        List<AttrAttrgroupRelationEntity> collect = attrGroupRelationVos.stream().map((obj) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(obj, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationDao.deleteBatch(collect);
    }

    @Override
    public Map<Long, List<Long>> getAttrGroupIds(List<Long> attrIds) {
        Map<Long, List<Long>> map = new HashMap<>();
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = baseMapper.getGroupIds(attrIds);
        attrAttrgroupRelationEntities.stream().forEach(item -> {

            Long attrGroupId = item.getAttrGroupId();
            Long attrId = item.getAttrId();
            if (map.get(attrGroupId) == null) {
                List<Long> longs = new ArrayList<>();
                longs.add(item.getAttrId());
                map.put(attrGroupId, longs);
            } else {
                map.get(attrGroupId).add(attrId);
            }
        });
        return map;
    }
}